<?php
/**
 * 商场特卖
 * @author Ice <iceinto@mallog.com.cn>
 */
class DiscountAction extends LoginAction
{
    protected $_type = 'discount';

    public function index()
    {
        $MallDisModel = M('MallDiscount');
        $Page = $this->getPage($MallDisModel->count());
        $malldis = $MallDisModel->limit($Page->firstRow . ',' . $Page->listRows)->order('itemid DESC')->select();
        $this->assign('malldis', $malldis);
        $this->assign('page', $Page->show());
        $this->display();
    }

    public function add()
    {
        $this->getLoadData();
        if (isPost()) {
            $data = array();
            $data = $this->getPostDate();
            $this->createAdd('MallDiscount', $data);
        }
        $this->display();
    }

    /**
     * 修改特卖信息
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function edit()
    {
        $id = intval(getGet('id'));
        if (!$id) {
            $this->error('信息获取必须提供数据标识！');
        }
        $this->getLoadData();
        $MallDiscountModel = M('MallDiscount');
        $dis = $MallDiscountModel->find($id);
        if (isPost()) {
            $data = $this->getPostDate(true);
            $data['itemid'] = $id;
            $rs = $this->createUpdate('MallDiscount', $data, false);
            if ($rs) {
                $this->successMessage('修改成功！');
            } else {
                $this->errorMessage('更新失败！');
            }
            if ($data['image'] && $rs) {
                if ($data['image'] != $dis['image']) {
                    $this->delUpLoadFile($dis['image'], $this->_type);
                }
            } else if (!$rs && $data['image']) {
                $this->delUpLoadFile($data['image'], $this->_type);
                $this->error('更新失败！');
            }
            $dis = $MallDiscountModel->find($id);
        }
        if ($dis) {
            //获取楼层信息
            $floor = M('Floor')->find($dis['fid']);
            $this->assign('floor', $floor);
            $this->assign('dis', $dis);
            //获取图片
            $imageUrl = $this->getManagedUrl($dis['image'], $this->_type);
            $this->assign('image_url', $imageUrl);
        } else {
            $this->error('特卖信息加载失败！');
        }
        $this->display();
    }

    /**
     * 加载需要加载的数据
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getLoadData()
    {
        $market = M('Market')->field('id,name')->select();
        if (!$market) {
            $this->error('商场列表不能为空！');
        }
        $brands = M('Brand')->field('id,name,initials')->order('initials')->select();
        $this->assign('market', $market);
        $this->assign('brands', $brands);
    }

    /**
     * 获取提交数据
     * @param bool $isEdit 是否是修改
     * @return array
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getPostDate($isEdit = false)
    {
        $data = array();
        $data['title'] = getPost('title');
        $data['detail'] = getPost('detail');
        $data['start_time'] = strtotime(getPost('start_time'));
        $data['end_time'] = strtotime(getPost('end_time'));
        $data['mid'] = intval(getPost('market'));
        $data['fid'] = intval(getPost('floor'));
        $data['bid'] = intval(getPost('brand'));
        $data['k1'] = getPost('k1');
        $data['k2'] = getPost('k2');
        $data['last_update'] = time();
        //优惠活动时间
        if ($data['start_time'] > $data['end_time']) {
            $this->error('特卖活动结束时间不能小于开始时间！');
        }
        //文件执行上传并且保存返回媒体库标示
        $doUpload = true;
        if ($isEdit) {
            if (!$_FILES['image']['name']) {
                $doUpload = false;
            }
        } else {
            $data['add_time'] = time();
            $data['editor'] = $_SESSION[C('USER_AUTH_KEY')];
        }
        if ($doUpload) {
            $fId = $this->uploadFiles($this->_type);
            $data['image'] = $fId; //媒体库标示
        }
        return $data;
    }
}

?>